<template>
	<el-header style="height: auto;">
		<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
	</el-header>
	<el-header>
		<div class="left-panel">
			<el-button-group>
			<el-button type="primary" icon="el-icon-position" @click="createBpm">创建</el-button>
			<el-button type="warning" icon="el-icon-folder-checked" :disabled="selection.length==0" @click="doCollection">收藏</el-button>
			</el-button-group>
		</div>
		<div class="right-panel">
			<el-form-item label="发起人" style="padding-top: 20px;" label-width="80px">
				<select-user-dialog v-model:accountId="search.createUser" :mode="false"/>
			</el-form-item>
			<el-form-item label="流水号" style="padding-top: 20px;" label-width="80px">
				<el-input v-model="search.bpmRunNo" placeholder="流水号" clearable></el-input>
			</el-form-item>
			<div class="right-panel-search">
				<el-input v-model="search.keyword" placeholder="流程标题" clearable></el-input>
				<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
			</div>
		</div>
	</el-header>
	<el-main class="nopadding">
		<scTable ref="table" :params="{model:'0'}" :apiObj="list.apiObj" @selection-change="selectionChange" row-key="runId" stripe remoteSort remoteFilter>
			<el-table-column type="selection" width="50"></el-table-column>
			<el-table-column label="流水号" prop="id" width="100" sortable='custom' align="center"></el-table-column>
			<el-table-column label="流程标题" prop="flowTitle" width="400" :show-overflow-tooltip="true">
				<template #default="scope">
					<div v-if="scope.row.recTime==''||scope.row.recTime==undefined" style="position: absolute;"><sc-icon-new/></div>
					{{ scope.row.flowTitle }}
				</template>
			</el-table-column>
			<el-table-column label="步骤名称" prop="stepName" width="200" align="center">
				<template #default="scope">
					【{{scope.row.stepName}}】
				</template>
			</el-table-column>
			<el-table-column label="转交人" prop="createUser" width="100" align="center"></el-table-column>
			<el-table-column label="转交时间" prop="createTime" width="150" align="center" sortable='custom'>
				<template #default="scope">
					<el-tag v-time="scope.row.createTime" format="yyyy-MM-dd hh:mm"></el-tag>
				</template>
			</el-table-column>
			<el-table-column label="状态" prop="passTime" width="100" align="center">
				<template #default="scope">
					<el-tag v-if="testPassTime(scope.row)=='1'" type="success" effect="dark">正常</el-tag>
					<el-tag v-else-if="testPassTime(scope.row)=='0'" type="danger" effect="dark">超时</el-tag>
					<el-tag v-else type="info" effect="dark">未知</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="紧急程度" prop="urgency" width="100" align="center">
				<template #default="scope">
					<el-tag v-if="scope.row.urgency==0" type="success">一般</el-tag>
					<el-tag v-else-if="scope.row.urgency==1" type="warning">紧急</el-tag>
					<el-tag v-else-if="scope.row.urgency==2" type="danger">加急</el-tag>
					<el-tag v-else type="info">未知</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="办理时限" prop="passTime" width="100">
				<template #default="scope">
					<el-tag v-if="scope.row.passTime==0" type="success" effect="dark">不限</el-tag>
					<el-tag v-else type="warning" effect="dark">{{scope.row.passTime}}小时</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="操作" fixed="right" align="right" width="170">
				<template #default="scope">
				<el-button-group>
					<el-button text type="primary" size="small" @click="handleBpm(scope.row, scope.$index)">办理</el-button>
					<el-button text type="primary" size="small" @click="changeUser(scope.row,scope.$index)" v-if="scope.row.freeToOther=='1'">委托</el-button>
					<el-button text type="primary" size="small" @click="sendToOther(scope.row, scope.$index)" v-if="scope.row.sendToRole=='1'">抄送</el-button>
					<el-popconfirm title="确定删除吗？" @confirm="deleteLogicBpm(scope.row, scope.$index)" v-if="scope.row.runTotal=='1'">
						<template #reference>
							<el-button text type="primary" size="small">删除</el-button>
						</template>
					</el-popconfirm>
					</el-button-group>
				</template>
			</el-table-column>
		</scTable>
	</el-main>
	<changeuserdialog v-if="Changeuserdialog.save" ref="changeUserDialog" @success="handleChangeSuccess" @closed="Changeuserdialog.save=false">
	</changeuserdialog>
	<bpm-collection v-if="collectDialog.save" ref="collectDialog" @closed="collectDialog.save=false"></bpm-collection>
	<sendToOther v-if="sendToDialog.save" ref="sendToDialog" @closed="sendToDialog.save=false"></sendToOther>
</template>
<script>
import scSelectFilter from '@/components/scSelectFilter'
import selectUserDialog from "@/views/module/selectUserDialog";
import Changeuserdialog from "@/views/bpm/approval/changeuserdialog";
import BpmCollection from "@/views/bpm/approval/bpmCollection";
import sendToOther from "@/views/bpm/handle/sendToOther.vue";
export default {
	name: 'Bpm Approval',
	components: {
		sendToOther,
		BpmCollection,
		Changeuserdialog,
		scSelectFilter,
		selectUserDialog
	},
	data() {
		return {
			Changeuserdialog:{
				save:false
			},
			collectDialog:{
				save:false
			},
			sendToDialog:{
				save:false
			},
			selection: [],
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "流程分类",
					key: "flowId",
					multiple: true,
					options:[{
						label: "全部",
						value: ""
					}]
				}
			],
			list: {
				apiObj: this.$API.bpm.bpmStepRun.getBpmFlowApprovalList
			},
			search: {
				model:'0',
				date:'',
				flowId:'',
				keyword: "",
				bpmRunNo:'',
				createUser:'',
			}
		}
	},
	mounted() {
		this.getDoingBpmFlowList();
	},
	methods: {
		doCollection()
		{
			var runIdArr=[];
			this.selection.forEach(item => {
				runIdArr.push(item.runId);
			})
			this.collectDialog.save = true
			this.$nextTick(() => {
				this.$refs.collectDialog.open().setData(runIdArr)
			})
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		//搜索
		upsearch(){
			this.$refs.table.upData(this.search)
		},
		filterChange(data){
			this.search.date = data.date;
			this.search.flowId = data.flowId;
			this.$refs.table.upData(this.search)
		},
		async getDoingBpmFlowList()
		{
			var res = await this.$API.bpm.bpmFlow.getDoingBpmFlowList.get({model:'0'});
			if (res.code == 200) {
				this.filterData[1].options = res.data
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		testPassTime(row){
			let createTime = row.createTime;
			let passTime = row.passTime;
			if(passTime!=0){
				let timestamp1 = Date.parse(new Date(createTime)) + passTime * 60*60*1000;
				let timestamp = Date.parse(new Date());
				if (timestamp1 > timestamp) {
					return "1"
				} else {
					return "0";
				}
			}else {
				return "1";
			}

		},
		handleBpm(row,index)
		{
			this.$nextTick(() => {
				this.$router.replace({
					path: '/bpm/handle/dowork',
					query:{
						runId:row.runId,
						stepRunId:row.stepRunId,
						flowId:row.flowId
					}
				})
			})
		},
		async deleteLogicBpm(row,index)
		{
			var res = await this.$API.bpm.bpmList.deleteLogicBpm.post({runId:row.runId});
			if (res.code == 200) {
				this.$message.success("操作成功");
				this.$refs.table.refresh();
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async changeUser(row,index)
		{
			this.Changeuserdialog.save = true
			this.$nextTick(() => {
				this.$refs.changeUserDialog.open().setData(row)
			})
		},
		async sendToOther(row,index){
			this.sendToDialog.save = true
			this.$nextTick(() => {
				this.$refs.sendToDialog.open().setData(row)
			})
		},
		handleChangeSuccess(){
			this.$refs.table.refresh();
		},
		createBpm(){
			this.$nextTick(() => {
				this.$router.replace({
					path: '/bpm/create',
				})
			})
		}
	}
}
</script>
